package com.lc.mode;

import com.lc.handler.ProtocolUtil;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @author liuc
 * @date 2021/5/5
 * @description 响应
 **/
public class Response {

    private SocketChannel channel;

    public Response(SocketChannel channel){
        this.channel = channel;
    }

    public void write(byte[] content) throws IOException {
        if (channel.isOpen()) {
            channel.write(ByteBuffer.wrap(content));
        }
    }

    public void write(String data) throws IOException {
        if (channel.isOpen()) {
            channel.write(ByteBuffer.wrap(ProtocolUtil.ok(data).getBytes()));
        }
    }

    public void notFound() throws IOException {
        if (channel.isOpen()) {
            channel.write(ByteBuffer.wrap(ProtocolUtil.notFound().getBytes()));
        }
    }
}
